package hou.just4fun.HupuTweets.work;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

import hou.just4fun.HupuTweets.model.User;
import hou.just4fun.HupuTweets.util.JedisPoolFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ScanResult;

/**
 * @author houweitao
 * @date 2016年5月13日 下午2:25:41
 */

public class MostActiveUser {
	private static final JedisPool pool = new JedisPoolFactory().getInstance();
	private static final String key = "HUPU$TWEET";

	public static void main(String[] args) {
		MostActiveUser ma = new MostActiveUser();
		ma.showUsers();
	}

	void showUsers() {
		List<User> users = getAllUser();
		Collections.sort(users, new UserComparator());
		for (User user : users)
			System.out.println(user);
		System.out.println("发表过碎碎念的用户数目:" + users.size());
	}

	@SuppressWarnings("deprecation")
	List<User> getAllUser() {
		List<User> ret = new ArrayList<>();

		Jedis jedis = pool.getResource();
		ScanResult<Entry<String, String>> res = null;
		int cruser = 0;
		boolean first = true;
		while (cruser != 0 || first) {
			first = false;
			res = jedis.hscan(key, cruser);
			cruser = res.getCursor();
			List<Entry<String, String>> list = res.getResult();
			for (Entry<String, String> entry : list) {
				System.out.println(entry.getKey());
				User user = JSON.parseObject(entry.getValue(), User.class);
				System.out.println(user);
				System.out.println("=======");
				ret.add(user);
			}
		}
		return ret;
	}

	class UserComparator implements Comparator<User> {

		@Override
		public int compare(User one, User two) {
			if (one.getTweetMap().size() > two.getTweetMap().size())
				return 1;
			else if (one.getTweetMap().size() < two.getTweetMap().size())
				return -1;
			else
				return 0;
		}

	}
}

/*[萧瑟:http://my.hupu.com/bleakrustle; 碎碎念数目:5]
[lizuosherlock:http://my.hupu.com/4561930; 碎碎念数目:5]
[KBxiang24:http://my.hupu.com/16109527; 碎碎念数目:5]
[WillyCock:http://my.hupu.com/5331909; 碎碎念数目:5]
[TD21永恒:http://my.hupu.com/16303677; 碎碎念数目:5]
[haruma:http://my.hupu.com/3504970; 碎碎念数目:5]
[刘背心教授:http://my.hupu.com/15521960; 碎碎念数目:5]
[apple_of_sodom:http://my.hupu.com/16113913; 碎碎念数目:5]
[小飞飞羊:http://my.hupu.com/4445894; 碎碎念数目:5]
[markzeng1025:http://my.hupu.com/thechosenonemark; 碎碎念数目:5]
[明日之牛:http://my.hupu.com/16718350; 碎碎念数目:5]
[sonycm7ti:http://my.hupu.com/17085499; 碎碎念数目:5]
[深蓝的海盗旗:http://my.hupu.com/431604; 碎碎念数目:5]
[weiye4869:http://my.hupu.com/4194266; 碎碎念数目:5]
[第九站诗人:http://my.hupu.com/18747474; 碎碎念数目:5]
[JERRY-SHF:http://my.hupu.com/JERRY-SHF; 碎碎念数目:5]
[lfsroy:http://my.hupu.com/18229000; 碎碎念数目:5]
[dafinest33:http://my.hupu.com/dafinest33; 碎碎念数目:5]
[m-KEES:http://my.hupu.com/m-KEES; 碎碎念数目:5]
[hoholzl:http://my.hupu.com/hoholzl; 碎碎念数目:5]
[锦舌郎君:http://my.hupu.com/26231829; 碎碎念数目:5]
[留住幸福:http://my.hupu.com/17744758; 碎碎念数目:6]
[Cavs_King:http://my.hupu.com/CavsKingJames; 碎碎念数目:6]
[AI093604:http://my.hupu.com/answergeng; 碎碎念数目:6]
[ron_z:http://my.hupu.com/2644325; 碎碎念数目:6]
[大皇蟒:http://my.hupu.com/dahuangmang; 碎碎念数目:6]
[koxyz39:http://my.hupu.com/18402118; 碎碎念数目:6]
[wanghuanchen:http://my.hupu.com/647852; 碎碎念数目:6]
[山治:http://my.hupu.com/OnePiece--Sanji; 碎碎念数目:6]
[赤蓝翼:http://my.hupu.com/rbw1007; 碎碎念数目:6]
[nieshu:http://my.hupu.com/ns333; 碎碎念数目:6]
[少侠杨尼克:http://my.hupu.com/YoungNick; 碎碎念数目:6]
[老科萌萌哒:http://my.hupu.com/20737062; 碎碎念数目:6]
[mjlwm23:http://my.hupu.com/mjlwm23; 碎碎念数目:6]
[尼采已死:http://my.hupu.com/604338; 碎碎念数目:7]
[梦回2008:http://my.hupu.com/18269208; 碎碎念数目:7]
[卫月:http://my.hupu.com/wyyw5526; 碎碎念数目:7]
[巴德施图伯:http://my.hupu.com/4397030; 碎碎念数目:7]
[�际米兰:http://my.hupu.com/17981256; 碎碎念数目:7]
[绝杀干拔麦:http://my.hupu.com/6460989; 碎碎念数目:7]
[wxp92403:http://my.hupu.com/4207821; 碎碎念数目:7]
[never.leave:http://my.hupu.com/496442; 碎碎念数目:7]
[他入我身:http://my.hupu.com/lww7; 碎碎念数目:8]
[为冠军攒RP:http://my.hupu.com/4900416; 碎碎念数目:8]
[渣神2011:http://my.hupu.com/5753561; 碎碎念数目:8]
[拉风王子我:http://my.hupu.com/25743647; 碎碎念数目:8]
[zjysuperme:http://my.hupu.com/asd523920; 碎碎念数目:8]
[jamison_33:http://my.hupu.com/166039; 碎碎念数目:9]
[火星中锋:http://my.hupu.com/15658031; 碎碎念数目:9]
[米高-左吨:http://my.hupu.com/3846125; 碎碎念数目:9]
[我只能呵呵:http://my.hupu.com/WoZhiNengHeHe; 碎碎念数目:10]
[体育迷小明:http://my.hupu.com/27822557; 碎碎念数目:11]
[kevin35mvp:http://my.hupu.com/kevin-35; 碎碎念数目:11]
[miracle004:http://my.hupu.com/miracle004; 碎碎念数目:11]
[johncena622:http://my.hupu.com/553270; 碎碎念数目:11]
[至爱戴维斯:http://my.hupu.com/377153; 碎碎念数目:12]
[多云转雨:http://my.hupu.com/27737586; 碎碎念数目:12]
[霸王硬上钩:http://my.hupu.com/L__Y; 碎碎念数目:12]
[康思坦丁马卡里奇:http://my.hupu.com/GSLQ; 碎碎念数目:13]
[吊死鬼真帅:http://my.hupu.com/212742; 碎碎念数目:13]
[Herr_Snow:http://my.hupu.com/25533481; 碎碎念数目:18]
[卡卡卡卡特:http://my.hupu.com/3045381; 碎碎念数目:18]
[旺旺旺旺大:http://my.hupu.com/18444988; 碎碎念数目:19]
[huangzhen32:http://my.hupu.com/3180358; 碎碎念数目:19]
[hpsailor:http://my.hupu.com/18386725; 碎碎念数目:22]
[TDKLNR:http://my.hupu.com/tdklnr; 碎碎念数目:23]
[themissed:http://my.hupu.com/4195070; 碎碎念数目:28]
[怎么停不下:http://my.hupu.com/18108379; 碎碎念数目:28]
[漫天雪花翼纷飞:http://my.hupu.com/25758991; 碎碎念数目:32]
[kb85:http://my.hupu.com/126853; 碎碎念数目:35]
[虎扑亮图:http://my.hupu.com/group/hupugif; 碎碎念数目:35]
[虎扑时钟:http://my.hupu.com/hoopclock; 碎碎念数目:60]
[哈杜比三巨头:http://my.hupu.com/28386542; 碎碎念数目:67]
[哈比杜三巨头:http://my.hupu.com/28040847; 碎碎念数目:68]*/
